package com.guashu.springbootdistributedid.demo;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class BatchImageToBase64Converter {

    public static void main(String[] args) {
        String inputFolderPath = "E:\\资料\\gw-资料";
        String outputFilePath = "E:\\资料\\gw-资料\\out.txt";

        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath));

            Files.walk(Paths.get(inputFolderPath))
                    .filter(Files::isRegularFile)
                    .filter(path -> path.toString().toLowerCase().endsWith(".png"))
                    .forEach(path -> {
                        try {
                            byte[] imageBytes = Files.readAllBytes(path);
                            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
                            writer.write("File: " + path.getFileName() + "\n");
                            writer.write(base64Image + "\n");
                            writer.write("----------\n");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });

            writer.close();
            System.out.println("Base64 representations of images have been written to out.txt.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
